package com.ishadows.notebook.model;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;
import com.app.library.adapter.ItemView;
import com.app.library.utils.BitmapUtil;
import com.app.library.utils.DateUtil;
import com.ishadows.notebook.R;
@Table(name="scrap_paper_model")
public class ScrapPaperModel extends Model implements ItemView{
	@Column(name="img_big")
	private byte [] imgBig;
	@Column(name="img_small")
	private byte [] imgSmall;
	@Column(name="content")
	private byte [] content;
	@Column(name="bg")
	private byte [] bg;
	@Column(name="date")
	private Date date;
	private boolean check;
	@Column(name="dirName")
	private String dirName;
	public byte[] getImgBig() {
		return imgBig;
	}
	public byte[] getImgSmall() {
		return imgSmall;
	}
	public byte[] getContent() {
		return content;
	}
	public byte[] getBg() {
		return bg;
	}
	public void setImgBig(byte[] imgBig) {
		this.imgBig = imgBig;
	}
	public void setImgSmall(byte[] imgSmall) {
		this.imgSmall = imgSmall;
	}
	public void setContent(byte[] content) {
		this.content = content;
	}
	public void setBg(byte[] bg) {
		this.bg = bg;
	}
	
	public void saveImg(Bitmap bitmap){
		Bitmap bp =Bitmap.createBitmap(bitmap);
		imgBig=BitmapUtil.bitmapToByte(bp, 50);
		imgSmall=BitmapUtil.bitmapToByte(BitmapUtil.MinimizeBitmap(bp, 200),100);
		date =new Date();
	}
	
	public void saveBg(Bitmap bitmap){
		bg=BitmapUtil.bitmapToByte(bitmap, 50);
	}
	@Override
	public View getView(Context context, int position, View convertView,
			ViewGroup parent) {
		if(convertView!=null){
			return convertView;
		}
		View view =LayoutInflater.from(context).inflate(R.layout.item_scrap_paper, null);
		TextView date =(TextView) view.findViewById(R.id.item_scrap_paper_date);
		ImageView image =(ImageView) view.findViewById(R.id.item_scrap_paper_img);
		date.setText(DateUtil.getDate(getDate()));
		image.setImageBitmap(BitmapUtil.byteToBitmap(imgSmall));
		CheckBox checks =(CheckBox) view.findViewById(R.id.item_scrap_paper_check);
		checks.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				check=isChecked;
			}
		});
		return view;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	public static List<ScrapPaperModel> getAllNote(String dirName){
		List<ScrapPaperModel> scraps =new Select().from(ScrapPaperModel.class).where("dirName =?", dirName).orderBy("date asc").execute();
		for(ScrapPaperModel scrap:scraps){
			scrap.setBg(null);
			scrap.setContent(null);
			scrap.setImgBig(null);
		}
		return scraps;
	}
	public boolean isCheck() {
		return check;
	}
	public void setCheck(boolean check) {
		this.check = check;
	}
	
	public String getDirName() {
		return dirName;
	}
	public void setDirName(String dirName) {
		this.dirName = dirName;
	}
	
	
	
	
	
	
	
}
